<template>
  <div class="member-detail">
    <van-nav-bar 
      title="成员详情" 
      left-text="返回"
      left-arrow
      @click-left="router.back()"
    />
    
    <div class="member-info" v-if="memberInfo">
      <van-cell-group inset>
        <van-cell title="姓名" :value="memberInfo.realName" />
        <van-cell title="电话" :value="memberInfo.phone" />
        <van-cell title="关系">
          <template #value>
            <van-tag :type="getRelationTagType(memberInfo.relationType)">
              {{ getRelationText(memberInfo.relationType) }}
            </van-tag>
          </template>
        </van-cell>
      </van-cell-group>
    </div>
    
    <div class="health-records">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-cell-group inset title="健康记录">
          <template v-if="!loading">
            <template v-if="records.length">
              <van-cell 
                v-for="record in records" 
                :key="record.id"
                :title="getRecordTypeText(record.recordType)"
                :value="formatValue(record)"
                :label="formatDateTime(record.recordTime)"
              >
                <template #value>
                  <span :class="getValueClass(record)">{{ formatValue(record) }}</span>
                </template>
              </van-cell>
            </template>
            <van-empty v-else description="暂无健康记录">
              <template #image>
                <van-icon name="records" size="60" color="#969799" />
              </template>
            </van-empty>
          </template>
          <van-loading v-else size="24px" vertical>加载中...</van-loading>
        </van-cell-group>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast } from 'vant'
import { getMemberHealthRecords } from '@/api/family'

const route = useRoute()
const router = useRouter()
const records = ref([])
const loading = ref(false)
const refreshing = ref(false)
const memberInfo = ref(null)

const recordTypeOptions = {
  1: '体重',
  2: '血压'
}

const relationOptions = {
  1: '父母',
  2: '子女',
  3: '配偶',
  4: '其他'
}

onMounted(async () => {
  await loadRecords()
})

const loadRecords = async () => {
  try {
    loading.value = true
    const { data } = await getMemberHealthRecords(route.params.id)
    records.value = data.records
    memberInfo.value = data.memberInfo
  } catch (error) {
    if (error.response?.status === 403) {
      showToast('无权查看该成员的健康记录')
      router.back()
    } else {
      showToast('获取健康记录失败')
    }
  } finally {
    loading.value = false
    refreshing.value = false
  }
}

const onRefresh = async () => {
  refreshing.value = true
  await loadRecords()
}

const getRecordTypeText = (type) => recordTypeOptions[type]

const formatValue = (record) => {
  if (record.recordType === 1) {
    return `${record.recordValue}kg`
  } else if (record.recordType === 2) {
    return `${record.recordValue}mmHg`
  }
  return record.recordValue
}

const formatDateTime = (time) => {
  return new Date(time).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const getValueClass = (record) => {
  return {
    'weight-value': record.recordType === 1,
    'bp-value': record.recordType === 2
  }
}

const getRelationText = (type) => relationOptions[type]

const getRelationTagType = (type) => {
  const types = {
    1: 'primary',
    2: 'success',
    3: 'warning',
    4: 'default'
  }
  return types[type]
}
</script>

<style lang="scss" scoped>
.member-detail {
  min-height: 100vh;
  background-color: #f7f8fa;
  
  .member-info {
    margin: 12px 0;
  }
  
  .health-records {
    padding: 12px;
    
    .van-cell-group {
      position: relative;
      min-height: 100px;
      
      .van-loading {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
    
    .weight-value {
      color: #1989fa;
    }
    
    .bp-value {
      color: #ee0a24;
    }
  }
}

@media screen and (min-width: 768px) {
  .member-detail {
    .member-info,
    .health-records {
      max-width: 600px;
      margin: 12px auto;
    }
  }
}
</style> 